package com.tuoren.forward.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tuoren.forward.annotation.HandleLog;
import com.tuoren.forward.constant.CommonConstant;
import com.tuoren.forward.entity.Role;
import com.tuoren.forward.entity.req.IdReq;
import com.tuoren.forward.entity.req.RoleAuthReq;
import com.tuoren.forward.entity.req.RoleSearchReq;
import com.tuoren.forward.service.RoleService;
import com.tuoren.forward.util.Result;
import com.tuoren.forward.util.ResultData;
import com.tuoren.forward.util.ResultPage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("role")
@Tag(name="角色接口")
public class RoleController {
	
	@Autowired
	RoleService roleServcie;
	
	@PostMapping("search")
    @ResponseBody
    @Operation(summary = "查询角色")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultPage<Role> search(@RequestBody RoleSearchReq req){
		log.info("search>>:{}",req);
		return roleServcie.search(req);
	} 
	
	@PostMapping("listAll")
    @ResponseBody
    @Operation(summary = "列出所有角色")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<List<Role>> listAll(){
		log.info("listAll>>");
		return roleServcie.listAll();
	} 

	@HandleLog
	@PostMapping("add")
	@ResponseBody
	@Operation(summary = "添加角色")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result  add(@RequestBody Role req) {
		log.info("add>>:{}"+req);
		return roleServcie.add(req);
	}
	
	@HandleLog
	@PostMapping("edit")
	@ResponseBody
	@Operation(summary = "修改角色")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result edit(@RequestBody Role req) {
		log.info("edit>>:{}",req);
		return roleServcie.edit(req);
	}
	
	@HandleLog
	@PostMapping("delete")
	@ResponseBody
	@Operation(summary = "删除角色")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result delete(@RequestBody IdReq req) {
		log.info("delete>>:{}",req);
		return roleServcie.delete(req.getId());
	}
	
	@HandleLog
	@PostMapping("authPermission")
	@ResponseBody
	@Operation(summary = "角色赋权")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result authPermission(@RequestBody RoleAuthReq req) {
		log.info("authorPermission>>:{}"+req);
		if(StringUtils.isEmpty(req.getRoleId())) {
			return Result.fail(CommonConstant.LACK_PARAM);
		}
		return roleServcie.authPermission(req);
	} 
}
